// pages/my/my.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // openid: "",
    // loginstate: "0",
    // openid: "",
    // userEntity: null,
    // terminal: "",
    // osVersion: "",
    // phoneNumber: "",
    // showModal: false, //定义登录弹窗
    
    show: false,
    goodsList: [],
    pageNum: 1,
    pageSize: 10,
    isLogin: false,
    username: '注册/登录',
    avatar: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsfy.eway.org.cn%2FImages%2Fblank.png&refer=http%3A%2F%2Fsfy.eway.org.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657521361&t=eb30c92be5eeb87ff5703eadb9821963'
  },
  showTra(){
    this.setData({
      show: !this.data.show
    })
    setTimeout(() => {
      this.setData({
        show: !this.data.show
      })
    }, 1000);
  },
  goToLogin: function (param) {
    wx.navigateTo({
      url: './login/login'
    })
  },
  userLogin: function () {
    let token = wx.getStorageSync('youxuan-user-token');
    if (!token) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
   // 获取滚动条当前位置
 onPageScroll: function (e) {
  if (e.scrollTop > 100) {
   this.setData({
    floorstatus: true
   });
  } else {
   this.setData({
    floorstatus: false
   });
  }
 },

 //回到顶部
 goTop: function (e) { // 一键回到顶部
  if (wx.pageScrollTo) {
   wx.pageScrollTo({
    scrollTop: 0
   })
  } else {
   wx.showModal({
    title: '提示',
    content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
   })
  }
 },
 onclickremind(){
  wx.showToast({
    title: '订阅提醒成功',
    icon: "none"
})
 },


  // 快捷登录
  // userLogin: function () {
  //     wx.getUserProfile({
  //             desc: '登录授权',
  //         })
  //         .then(res => {
  //             console.log("允许登录", res.userInfo);
  //             this.setData({
  //                 nickName: res.userInfo.nickName,
  //                 avatarUrl: res.userInfo.avatarUrl
  //             });
  //             wx.setStorageSync('userInfo', res.userInfo)
  //         })
  //         .catch(err => {
  //             console.log("拒绝登录", err);
  //         });
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let userInfo = wx.getStorageSync('youxuan-user-userInfo')
    if (userInfo) {
      this.setData({
        avatar: userInfo.avatar,
        username: userInfo.nickName
      })
    }

    var goodsListResult = await request("/goods/list?pageNum=" + this.data.pageNum + "&pageSize=" + this.data.pageSize)
    console.log(goodsListResult)
    this.setData({
      goodsList: goodsListResult.data.rows
    })

  },

  toGoodsDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/goodsInfo/goodsInfo?gid=' + e.currentTarget.dataset.gid,
    })
  },
  async addCartGoods(e) {
    let gid = e.currentTarget.dataset.gid;
    let userInfo = wx.getStorageSync('youxuan-user-userInfo')
    console.log(userInfo.id, gid)
    let result = await request("/cart/add/goods", {
      goodsId: gid,
      num: 1,
      userId: userInfo.id
    }, "POST")
    console.log(result)
    if (result.code == 200) {
      wx.showToast({
        title: '添加成功',
        icon: "none"
      })
    }
  },
  toAllOrderList(){
    let token =wx.getStorageSync('youxuan-user-token');
    if (token) {
      wx.navigateTo({
        url: '/pages/orderList/orderList',
      })  
    }else{
      this.showTra()
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    var goodsListResult = await request("/goods/list?pageNum=" + this.data.pageNum + "&pageSize=" + this.data.pageSize)
    console.log('触底了', goodsListResult.data.rows.length)
    if (goodsListResult.data.rows.length > 0) {
      this.setData({
        goodsList: this.data.goodsList.concat(goodsListResult.data.rows)
      })
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})